package GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import 学生.*;
import 数据库和网络.*;

public class studentWindow extends JFrame implements Runnable{
    JPanel mainPanel;

    public studentWindow(){}

    public studentWindow(String a){
        mainPanel = new JPanel();

        this.setSize(500,420);
        mainPanel.setSize(this.getSize());
        this.setContentPane(mainPanel);
        this.setLocationRelativeTo(null);//居中
        this.setTitle("学生在线考试平台");
        this.setVisible(true);
        // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Runnable s=new Runnable(){
            @Override
            public void run(){
                String name="";
                String banji="";
                try {
                    myDatebase nameGetter=new myDatebase("student");
                    String sql="select * from student.student where StudentID='"+enterWindow.IDText.getText()+"'";
                    nameGetter.statement.execute(sql);
                    nameGetter.resultSet=nameGetter.statement.executeQuery(sql);
                    while(nameGetter.resultSet.next()){
                    name=nameGetter.resultSet.getString(1);
                    banji=nameGetter.resultSet.getString(4);}
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                JLabel infoLabel = new JLabel("姓名："+name+"   学号："+enterWindow.IDText.getText()+"   班级："+banji);
                infoLabel.setBounds(20,50,300,40);
                JButton testButton = new JButton("开始考试");
                JButton rankButton = new JButton("查看排名");
                JLabel label2 = new JLabel();
                JButton backButton = new JButton("退出登录");
                mainPanel.setLayout(null);

                testButton.setBounds(70,150,150,80);
                mainPanel.add(testButton);
                rankButton.setBounds(280,150,150,80);
                mainPanel.add(rankButton);
                mainPanel.add(label2);
                backButton.setBounds(305,280,110,40);
                mainPanel.add(backButton);
                testButton.setBackground(Color.lightGray);
                rankButton.setBackground(Color.lightGray);
                backButton.setBackground(Color.lightGray);
                testButton.addActionListener(new testListener());
                rankButton.addActionListener(new rankListener());
                backButton.addActionListener(new backListener());
                mainPanel.add(infoLabel);
                mainPanel.add(testButton);
                mainPanel.add(rankButton);
                mainPanel.add(backButton);
                infoLabel.setHorizontalAlignment(SwingConstants.CENTER);
                infoLabel.setVerticalAlignment(SwingConstants.CENTER);

                enterWindow.currentStudent.setName(name);
                enterWindow.currentStudent.setSchoolID(enterWindow.IDText.getText());
                enterWindow.currentStudent.setMyClass(banji);

            }
        };
        Thread thread2=new Thread(s);
        thread2.run();

    }

    public class testListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            studentWindow.this.dispose();
            new testWindow();
        }
    }

    public class rankListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            studentWindow.this.dispose();
            studentRank k =new studentRank();
            k.setVisible(true);
        }
    }

    public class backListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {//关闭注册界面
            studentWindow.this.dispose();
            studentWindow.this.dispose();
            new enterWindow();
        }
    }

    @Override
    public void run(){
        new studentWindow("a");
    }
}
